﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Definition;
using Besture.Common.Helper;
using Besture.Common.Mail;
using Besture.Model;
using Besture.UI.Common;

namespace Besture.UI.Portal.UserCenter
{
    public partial class AjaxUserInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int returnResult = 0;
            switch (Request.Form["requestMethod"])
            {
                case "PhoneSendSmsSend":
                    //发送短信 SendSms
                    returnResult = PhoneSendSmsSend();
                    break;
                case "SendEmail":
                    //发送邮件 SendEmail
                    returnResult = 0;
                    break;
                case "ajax_phone":
                    //校验手机号唯一性
                    returnResult = GetPhoneEmail(Request.Form["Phone"], null, Request.Form["UserType"]);
                    break;
                case "ajax_email":
                    //校验邮件唯一性
                    returnResult = GetPhoneEmail(Request.Form["Email"], null, Request.Form["UserType"]);
                    break;
                case "UserInfo":
                    //修改手机和邮件方法
                    returnResult = BtnSaveClick();
                    break;
                default:
                    break;
            }
            Response.Write(returnResult);
            Response.End(); 
        }


        /// <summary>
        /// 发送邮件验证码
        /// </summary>
        public void EmailFindPass()
        {
            string email = StringHelper.Query("email");
            UserBaseModel userBaseModel = new UserBaseDac().GetDataByUser(email); 
            string emailTitle = XmlHelper.GetContentByNode(AppSettings.GetMsgPath(), Constants.EMAIL_FINDPASS_TITLE);
            string emailContant = XmlHelper.GetContentByNode(AppSettings.GetMsgPath(), Constants.EMAIL_FINDPASS_CONTENT);
            MailHelper.SendEmail(MailHelper.InitMailMessage(emailTitle, email, string.Format(emailContant,
                "http://" + Request.Url.Authority + "/Portal/FindUserPwd/EmailFindThree.aspx?user=" +
                EncryptHelper.Encrypt(userBaseModel.ID.ToString())), null));

        }


        //校验手机号邮件唯一性
        private int GetPhoneEmail(string sPhone, string sEmail, string sUserType)
        {
            if (!string.IsNullOrEmpty(sPhone))
                return new UserBaseDac().GetDataTableByPhoneOrEmail(sPhone, Convert.ToInt32(sUserType));
            else
                return new UserBaseDac().GetDataTableByPhoneOrEmail(sEmail, Convert.ToInt32(sUserType));
            return 0;
        } 

        public int PhoneSendSmsSend()
        {
            try
            {

                string code = (new Random().Next(000000, 999999)).ToString();
                Session["registerPhoneCode"] = code;
                new SendSms().SendMsg(AppSettings.SmsName, Request.Form["Phone"], AppSettings.SmsSignature + code, "5");
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
            return 0;
        }

        /// <summary>
        /// 会员中心 调用
        /// </summary>
        private int BtnSaveClick()
        {
            if (UserPage.GetUserLoginModel() != null)
            { 
                UserBaseModel userModel = null;
                var baseDac = new BaseDac<UserBaseModel>();
                userModel = new UserBaseDac().GetModel(UserPage.GetUserLoginModel().ID);
                if (userModel != null)
                {
                    userModel.Phone = Request.Form["Phone"];
                    userModel.Email = Request.Form["Email"];
                    userModel.Birthday = DateTime.Now;
                    userModel.StatusTime = DateTime.Now;
                    //加载修改基本数据
                    baseDac.LoadUpdateBaseModel(userModel);
                    if (new UserBaseDac().Update(userModel) > 0)
                    {
                        Session[Constants.SESSION_WEB_USER_MODEL] = userModel;
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            return 0;
        }
    }
}